package com.ttai.presenter.activity;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.ttai.model.dao.DBHelper;
import com.ttai.model.dao.bean.Id_Item;
import com.ttai.model.net.ArrayInfo;
import com.ttai.model.net.IdAcountBean;
import com.ttai.model.net.ResponseInfo;
import com.ttai.presenter.base.BaseArrayPresenter;
import com.ttai.presenter.base.BasePresenter;
import com.ttai.ui.activity.EnterPage;
import com.ttai.ui.activity.LoginId;
import com.ttai.ui.activity.VerifyLogin;
import com.ttai.ui.activity.VerifyMiYao;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

public class InsertIdAcountPresenter extends BasePresenter {
    LoginId loginId;
    ArrayList<Id_Item> idList = new ArrayList<>();

    public InsertIdAcountPresenter(LoginId loginId) {
        this.loginId = loginId;
    }

    public ArrayList<Id_Item> getList() {
        return idList;
    }



    @Override
    protected void showError(String message) {
        Toast.makeText(loginId,message,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void parseJson(JsonObject json) {
        Gson gson = new Gson();
        IdAcountBean idAcountBean = gson.fromJson(json,IdAcountBean.class);
        ArrayList<IdAcountBean.AccountBean> idAcountList = (ArrayList<IdAcountBean.AccountBean>) idAcountBean.getAccount();
        for (int i = 0; i < idAcountList.size(); i++) {
            String nameNumber = idAcountList.get(i).getAppName();
            String img = idAcountList.get(i).getIcon();
            String appid = String.valueOf(idAcountList.get(i).getAppid());
            String mynumber = idAcountList.get(i).getUseraccount();
            String logo = idAcountList.get(i).getAccountflag();

            Id_Item id_item = new Id_Item(img,nameNumber,mynumber,logo);
            idList.add(id_item);


        }
            loginId.getLoginIdRecycleViewAdapter().setData(idList);
    }

    public void storeIdAcount(String token){
        Call<ResponseInfo> idTokenInfo = responseInfoApi.restoreIdAcount(token);
        idTokenInfo.enqueue(new CallBackAdapter());
    }
//    LoginId loginId;
//    VerifyLogin verifyLogin;
//    VerifyMiYao verifyMiYao;
//
//    private ArrayList<Id_Item> idList = new ArrayList<>();
//
//    public ArrayList<Id_Item> getList() {
//        return idList;
//    }
//
//    public InsertIdAcountPresenter(LoginId loginId) {
//        this.loginId = loginId;
//    }
//
//    @Override
//    protected void parseArray(JsonArray data) {
//        String dataArray = data.toString();
//        ArrayList<IdAcountBean> idAcountList = new ArrayList<>();
//        Gson gson = new Gson();
//        idAcountList = gson.fromJson(dataArray,new TypeToken<ArrayList<IdAcountBean>>(){}.getType());
//
//        for (int i = 0; i < idAcountList.size(); i++) {
////            String phone = idAcountList.get(i).getTsAccount();
//            String nameNumber = idAcountList.get(i).getAppName();
//            String img = idAcountList.get(i).getIcon();
//            String appid = String.valueOf(idAcountList.get(i).getAppid());
//            String mynumber = idAcountList.get(i).getUseraccount();
//            String logo = idAcountList.get(i).getAccountflag();
//
//            Id_Item id_item = new Id_Item(img,nameNumber,mynumber,logo);
//            idList.add(id_item);
//
//            loginId.getLoginIdRecycleViewAdapter().setData(idList);
//
//
////            DBHelper dbHelper = DBHelper.getInstance(enterPage);
////            SQLiteDatabase db = dbHelper.getWritableDatabase();
////            dbHelper.getWritableDatabase();
////            ContentValues values = new ContentValues();
////            values.put("phone", phone);
////            values.put("appname", nameNumber);
////            values.put("appid", appid);
////            values.put("imgurl",img);
////            values.put("acountnumber", mynumber);
////            values.put("acountnumberlogo", logo);
////            db.insert("idacount", null, values);
////            values.clear();
//        }
//
//    }
//
//    @Override
//    protected void showError(String message) {
//        Toast.makeText(loginId,message,Toast.LENGTH_SHORT).show();
//    }
//
//        public void storeIdAcount(String token){
//        Call<ArrayInfo> idAcountInfo = responseInfoApi.restoreIdAcount(token);
//        idAcountInfo.enqueue(new CallBackArrayAdapter());
//    }
}
